package FebruaryOn2022;

/**
 * Copyright (c) XuQunXing Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Description:
 * User: x30003433
 * Date: 2022-02-08
 * Time: 11:28
 */
public class ReversePrefixOfWord {
    public static void main(String[] args) {
        System.out.println(reversePrefix1("abcdefd",'d'));
        System.out.println(reversePrefix1("xyxzxe",'z'));
        System.out.println(reversePrefix1("abcd",'z'));
    }
    public static String reversePrefix(String word, char ch) {
        // 1.找到下标的位置
        int loc = word.indexOf(ch);
        if (loc == -1) {
            return word;
        }
        // 2.翻转拼接
        StringBuilder sb = new StringBuilder(word.substring(0, loc+1));
        sb.reverse().append(word.substring(loc+1));
        return sb.toString();
    }
    public static String reversePrefix1(String word, char ch) {
        // 1.找到下标的位置
        int loc = word.indexOf(ch);
        if (loc > 0) {
            char[] chars = word.toCharArray();
            int left = 0, right = loc;
            while (left < right) {
                char tmp = chars[left];
                chars[left] = chars[right];
                chars[right] = tmp;
                left++;
                right--;
            }
            word = new String(chars);
        }
        return word;
    }
}
